package com.iqiyi.pps.epg.core.rpc.broadcast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iqiyi.pps.epg.api.model.web.broadcast.TPlatformControl;
import com.iqiyi.pps.epg.api.model.web.broadcast.TProgramPlatformControl;
import com.iqiyi.pps.epg.api.server.web.broadcast.BroadcastControlWebService;
import com.iqiyi.pps.epg.api.server.web.channel.ChannelWebService;
import com.iqiyi.pps.epg.core.model.channel.ChannelBase;
import com.iqiyi.pps.epg.core.model.channel.ChannelConfig;
import com.iqiyi.pps.epg.core.model.control.PlatformControl;
import com.iqiyi.pps.epg.core.model.control.ProgramPlatformControl;
import com.iqiyi.pps.epg.core.service.broadcast.BroadcastControlService;
import com.iqiyi.pps.epg.core.service.channel.ChannelService;
import com.iqiyi.pps.epg.core.service.program.IProgramAsyncService;
import com.iqiyi.pps.epg.core.utils.ApplicationContextUtil;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class BroadcastControlWebServiceImpl implements BroadcastControlWebService.Iface {
    private static final Logger logger = LoggerFactory.getLogger(BroadcastControlWebServiceImpl.class);

    @Override
    public boolean sets(long channelId, List<TPlatformControl> platforms, boolean assignToPrograms) throws TException {
        ChannelService channelService = (ChannelService) ApplicationContextUtil.getBean(ChannelService.class);
        List<ChannelConfig> playPlatformList = channelService.getConfig(ChannelConfig.PLAY_PLATFORM);
        Map<Integer, String> platformMap = new HashMap();
        for (ChannelConfig item : playPlatformList) {
            platformMap.put(item.getKey(), item.getVal());
        }
        List<PlatformControl> platformControls = new ArrayList();
        for (TPlatformControl item : platforms) {
            // 校验平台
            if (!platformMap.containsKey(item.getPlatform())) {
                return false;
            }
            PlatformControl entity = PlatformControl.thriftToEntity(item);
            platformControls.add(entity);
        }

        BroadcastControlService service = (BroadcastControlService) ApplicationContextUtil.getBean(BroadcastControlService.class);

        boolean flag = service.sets(channelId, platformControls);
        if (flag && assignToPrograms) {
            ChannelBase channelBase = channelService.getChannelBase(channelId);
            IProgramAsyncService programAsyncService = (IProgramAsyncService) ApplicationContextUtil.getBean(IProgramAsyncService.class);
            programAsyncService.addOverrideBroadcastControl(channelId ,platformControls,ChannelBase.LIVE_TYPE_LS ==channelBase.getLiveType().intValue());
        }
        return flag;
    }


    @Override
    public List<TPlatformControl> get(long channelId) throws TException {
        BroadcastControlService service = (BroadcastControlService) ApplicationContextUtil.getBean(BroadcastControlService.class);

        List<PlatformControl> platforms = service.getPlatformControls(channelId);
        List<TPlatformControl> tPlatforms = new ArrayList();
        if (platforms != null) {
            for (PlatformControl item : platforms) {
                tPlatforms.add(item.entityToThrift());
            }
        }

        return tPlatforms;
    }

    @Override
    public boolean setProgramPlatControls(long programId, List<TProgramPlatformControl> platforms) throws TException {
        logger.info("[BroadcastControlWebServiceImpl][setProgramPlatControls][programId={}][platforms={}]", programId, JSON.toJSONString(platforms));
        ChannelService channelService = (ChannelService) ApplicationContextUtil.getBean(ChannelService.class);
        List<ChannelConfig> playPlatformList = channelService.getConfig(ChannelConfig.PLAY_PLATFORM);
        Map<Integer, String> platformMap = new HashMap();
        for (ChannelConfig item : playPlatformList) {
            platformMap.put(item.getKey(), item.getVal());
        }
        //  List<ProgramPlatformControl> platformControls = new ArrayList();
        Map<Integer, ProgramPlatformControl> platformControls = new HashMap<Integer, ProgramPlatformControl>();

        if (platforms.size() == 1 && platforms.get(0).getPlatform() == 0) {
            ProgramPlatformControl entity = ProgramPlatformControl.thriftToEntity(platforms.get(0));
            for (Iterator<Integer> iterator = platformMap.keySet().iterator(); iterator.hasNext(); ) {
                ProgramPlatformControl temp = (ProgramPlatformControl) entity.clone();
                Integer k = iterator.next();
                temp.setPlayPlatform(k);
                platformControls.put(k, temp);
            }
        } else {
            for (TProgramPlatformControl item : platforms) {
                if (!platformMap.containsKey(item.getPlatform())) {
                    return false;
                }
                ProgramPlatformControl entity = ProgramPlatformControl.thriftToEntity(item);
                platformControls.put(entity.getPlayPlatform(),entity);
            }
        }

        BroadcastControlService service = (BroadcastControlService) ApplicationContextUtil.getBean(BroadcastControlService.class);
        return service.setsWithProgram(programId, platformControls);
    }

    @Override
    public List<TProgramPlatformControl> getProgramPlatControls(long programId) throws TException {
        BroadcastControlService service = (BroadcastControlService) ApplicationContextUtil.getBean(BroadcastControlService.class);
        List<ProgramPlatformControl> platforms = service.getProgramPlatformControls(programId);
        List<TProgramPlatformControl> tPlatforms = new ArrayList();
        if (null != platforms) {
            for (ProgramPlatformControl item : platforms) {
                tPlatforms.add(item.entityToThrift());
            }
        }
        return tPlatforms;
    }
}
